package com.xinzhidi.web.netty.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.web.netty.entity.AgreementCommand;
import com.xinzhidi.web.netty.entity.po.AgreementCommandPo;
import com.xinzhidi.web.netty.mapper.AgreementCommandMapper;
import com.xinzhidi.web.netty.utils.HexadecimalUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 协议指令表Service业务层处理
 *
 * @author cx
 * @date 2023-06-09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AgreementCommandService extends ServiceImpl<AgreementCommandMapper, AgreementCommand> {

    private final AgreementCommandMapper agreementCommandMapper;

    /**
     * 查询协议列表
     *
     * @param po 参数
     * @return 协议分页列表
     */
    public TableDataInfo<AgreementCommand> listAgreementCommand(AgreementCommandPo po) {
        List<AgreementCommand> list = this.list(this.buildGenTableQueryWrapper(po));
        return TableDataInfo.getDataTable(list);
    }


    /**
     * 获取协议指令
     *
     * @param agreementCommandPo
     * @return 协议数据
     */
    public AgreementCommand getProtocolInstruction(AgreementCommandPo agreementCommandPo) {
        AgreementCommand agreementCommand = this.getOne(this.buildGenTableQueryWrapper(agreementCommandPo));
        return agreementCommand;
    }

    /**
     * 组装查询条件
     *
     * @param commandPo 查询条件po
     * @return 协议指令
     */
    private LambdaQueryWrapper<AgreementCommand> buildGenTableQueryWrapper(AgreementCommandPo commandPo) {
        LambdaQueryWrapper<AgreementCommand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(commandPo.getId() != null, AgreementCommand::getId, commandPo.getId())
                .eq(StringUtils.isNotBlank(commandPo.getProtocolType()), AgreementCommand::getProtocolType, commandPo.getProtocolType())
                .eq(StringUtils.isNotBlank(commandPo.getType()), AgreementCommand::getType, commandPo.getType())
                .like(StringUtils.isNotBlank(commandPo.getProtocolName()), AgreementCommand::getProtocolName, commandPo.getProtocolName())
                .eq(StringUtils.isNotBlank(commandPo.getInstructType()), AgreementCommand::getInstructType, commandPo.getInstructType())
                .like(StringUtils.isNotBlank(commandPo.getInstructName()), AgreementCommand::getInstructName, commandPo.getInstructName());
        return wrapper;
    }

    /**
     * 组装户用阀门指令
     *
     * @param command 协议
     * @param address 地址
     * @param value   值（十六进制、可以是多个字段的值拼接的）
     * @return 指令
     */
    public String assemblyHouseholdValveInstruction(AgreementCommand command, String address, String value) {
        String message = "";
        message = command.getPrefix() + command.getStartChar() + command.getDeviceType() + address
                + command.getControlCode() + command.getLengthField() + command.getDataFieldInstruct() + command.getDataMark() + value;
        //获取校验码
        String checkCode = HexadecimalUtil.getCheckCode(message);
        message += checkCode + command.getEndMark();
        return message;
    }

    /**
     * 组装单元阀门指令
     *
     * @param command 协议
     * @param address 地址
     * @param ser     序列号
     * @param value   值（十六进制、可以是多个字段的值拼接的）
     * @return 指令
     */
    public String assemblyUnitValveInstruction(AgreementCommand command, String address, String ser, String value) {
        String message = "";
        message = command.getPrefix() + command.getStartChar() + command.getDeviceType() + address
                + command.getControlCode() + command.getLengthField() + command.getDataFieldInstruct() + command.getDataMark() + ser + value;
        //获取校验码
        String checkCode = HexadecimalUtil.getCheckCode(message);
        message += checkCode + command.getEndMark();
        return message;
    }

    @Cacheable(value = "agreementCommand", key = "'list_all'")
    public List<AgreementCommand> selectAll(){
        log.info("执行查库操作！");
        List<AgreementCommand> list =this.list();
        return list;
    }

    /**
     * 获取发送协议指令
     * @return
     */
    public AgreementCommand getSendAgreementCommand(String protocolType,String type,String instructType){
        List<AgreementCommand> list = this.selectAll();
        AgreementCommand agreementCommand = list.stream().filter(agreementCommand1 -> agreementCommand1.getProtocolType().equals(protocolType)
                && agreementCommand1.getType().equals(type) && agreementCommand1.getInstructType().equals(instructType)).findFirst().orElse(null);
        return agreementCommand;
    }


}
